#include "WebPage.h"
#include "tinyxml2.h"
#include <iostream>
#include <regex>
#include <string>

using std::cout;
using std::endl;
using namespace tinyxml2;

WebPage::WebPage(string &docstr/*Configuration &config WordSegmentation &jieba*/){

    // int pos = docstr.find("&$<br>&$");
    // if(pos != string::npos){
    //     docstr.replace(pos, 8, "  ");
    // }
    // pos = docstr.find("&nbsp;");
    // if(pos != string::npos){
    //     docstr.replace(pos, 6, "  ");
    // }

    //static int testCount = 0;
    //testCount++;

    tinyxml2::XMLDocument doc;
    doc.Parse(docstr.c_str());

    tinyxml2::XMLElement *rootElement = doc.RootElement();
    if(rootElement == NULL){
        // static int errorNum = 0;
        // errorNum++;
        // cout<<"errorNum: "<<errorNum<<endl;

        //cout<<"处理第"<<testCount<<"篇文档时  tinyxml2读取错误"<<endl;
        //cout<<"docstr: "<<docstr<<endl;

        _docId = -1;
        _docTitle = "";
        _docUrl = "";
    
        _docContent = "";

    }    
    else{
        string id = rootElement->FirstChildElement("docid")->GetText();
        _docId = stoi(id);

        string title = rootElement->FirstChildElement("title")->GetText();
        _docTitle = title;

        string url = rootElement->FirstChildElement("link")->GetText();
        _docUrl = url;

        string content = rootElement->FirstChildElement("content")->GetText();
        _docContent = content;

    }
    

}

WebPage::~WebPage(){}

int WebPage::getDocId() const{
    return _docId;
}

string WebPage::getTitle() const{
    return _docTitle;
}

string WebPage::getContent() const {
    return _docContent;
}

string WebPage::getUrl() const{
    return _docUrl;
}

//map<string, int> &WebPage::getWordsMap(){   //获取文档的词频统计map
//}

//对格式化文档进行处理
//void processDoc(const string &doc, /*Configuration &config*/ WordSegmentation &){
//}

//void WebPage::calcTopK(vector<string> &wordsVec, int k,
//                 set<string> &stopWordList){ //求取文档的topk词集
//}
